<template>
    <div>
        <el-dialog v-model="props.visible" title="车间列表" width="60%" @close="handleCancel">
            <el-form :model="queryParams" ref="queryRef" :inline="true">
                <el-form-item label="车间编号" prop="code">
                    <el-input v-model.trim="queryParams.code" placeholder="请输入车间编码" @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="车间名称" prop="name">
                    <el-input v-model.trim="queryParams.name" placeholder="请输入车间名称" @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="科目" prop="subjectType">
                    <el-select v-model="queryParams.subjectType" style="width: 200px;" placeholder="请选择科目">
                        <el-option v-for="item in ems_workshop_subject_type" :key="item.value" :value="item.value"
                            :label="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="userList" stripe border style="cursor: pointer;"
                @cell-click="handleCellClick">
                <el-table-column label="序号" width="80" type="index" align="center" />
                <el-table-column label="车间编码" min-width="120" align="center" :show-overflow-tooltip="true" prop="code">
                </el-table-column>
                <el-table-column label="车间名称" align="center" :show-overflow-tooltip="true" prop="name">
                </el-table-column>
                <el-table-column label="负责人" align="center" :show-overflow-tooltip="true" prop="chargePerson">
                </el-table-column>
                <el-table-column label="负责人电话" align="center" :show-overflow-tooltip="true" prop="chargePersonTel">
                </el-table-column>
                <el-table-column label="科目" align="center" :show-overflow-tooltip="true" prop="telephone">
                    <template v-slot="{ row }">
                        <dict-tag :options="ems_workshop_subject_type" :value="row.subjectType" />
                    </template>
                </el-table-column>
            </el-table>
            <div class="footer">
                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
const emit = defineEmits(['changeUpdate'])
import { getMesWorkShop } from '@/api/basicInfo/workshop'
const { proxy } = getCurrentInstance();
const { ems_workshop_subject_type } = proxy.useDict("ems_workshop_subject_type");

const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },

})
watch(() => props.visible, (val) => {
    if (val) {
        handleQuery()
    }
})
const userList = ref([])
const loading = ref(false)
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    subjectType: undefined,
    code: undefined
})
const total = ref(0)
// 选中当前行
const handleCellClick = (row) => {
    proxy.$modal.confirm('是否确认选择「' + row.name + '」').then(function () {
        emit('changeUpdate', row)
    }).catch(() => { });
}
const handleCancel = () => {
    proxy.resetForm("queryRef");
    emit('cancel')
}
const getList = async () => {
    loading.value = true
    const res = await getMesWorkShop(queryParams.value)
    loading.value = false
    userList.value = res.rows
    total.value = res.total
}
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};
// getList()
</script>

<style lang="scss" scoped></style>